// 引入
const bmap = require('../static/js/bmap-wx.min.js')

// 创建实例
const BMap = new bmap.BMapWX({
    ak: '2Yq9VjJk2YemCi8QdVuREyuJxar0Q6m5' //百度地图密钥
})


// 检查是否有地图对象
const checkBMap = () => {
    if (!BMap) {
        console.log('地图对象不存在')
        return false
    } else {
        console.log('地图对象存在')
        return true
    }
}

// 根据地址获取经纬度
export const getGeocode = async (address: string): Promise<any> => {
    return new Promise((resolve, reject) => {
        if (!checkBMap()) reject('地图初始化错误')
        BMap.geocoding({
            address,
            success: (data: any) => {
                resolve(data)
            },
            fail: (err: any) => {
                reject(err)
            }
        })
    })
}


// 根据经纬度获取地址
export const getAddress = async (lat: number, lng: number): Promise<any> => {
    return new Promise((resolve, reject) => {
        if (!checkBMap()) reject('地图初始化错误')
        BMap.regeocoding({
            location: `${lat},${lng}`,
            success: (data: any) => {
                resolve(data)
            },
            fail: (err: any) => {
                reject(err)
            }
        })
    })
}



